package rulebase;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;

import lexicon.Lexicon;
import lexicon.LexiconList;
import managers.LexiconManager;

import controller.MainController;

public class TriggerWordFrame extends JFrame{
	
	private JButton newWordBtn;
	private JButton okBtn;
	private JButton cancelBtn;
	private JComboBox cbxPOS;
	private JComboBox cbxWord;
	
	LexiconList lexList;
	
	public TriggerWordFrame(){
		getContentPane().setLayout(null);
		setSize(450,150);
		setResizable(false);
		setLocationRelativeTo(null);
		
		addComponents();
		setVisible(true);
	}
	
	public void addComponents(){
		newWordBtn = new JButton("New Word");
		newWordBtn.setBounds(220, 10, 120, 20);
		
		okBtn = new JButton("OK");
		okBtn.setBounds(110, 90, 100, 20);
		
		cancelBtn = new JButton("Cancel");
		cancelBtn.setBounds(232, 90, 100, 20);
		
		cbxPOS = new JComboBox();
		cbxPOS.setBounds(110, 10, 100, 20);
		cbxPOS.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				populateList();
				
			}
		});
		
		cbxWord = new JComboBox();
		cbxWord.setBounds(60, 51, 324, 20);
		
		populatePOS();
		populateList();
		
		getContentPane().add(cbxPOS);
		getContentPane().add(newWordBtn);
		getContentPane().add(cbxWord);
		getContentPane().add(okBtn);
		getContentPane().add(cancelBtn);
		
	}
	
	public boolean isLexiconExist(String newLexName, int row){
		
		for(int i = 0 ; i < lexList.getLexiconList().size();i++){
			if(lexList.getLexiconList().get(i).getName().equals(newLexName) && row!= i)
				return true;
		}
		return false;
	}
	
	public void populateList(){
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		
		lexList = LexiconManager.getInstance().getLexiconList(cbxPOS.getSelectedItem().toString());
		
		if(lexList != null){
			for(Lexicon lex : lexList.getLexiconList()){
				model.addElement(lex.getName() + " - " + lex.getGloss());
			}
		}
		
		cbxWord.setModel(model);
	}
	
	public void populatePOS(){
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		
			model.addElement("n");
			model.addElement("v");
			model.addElement("adj");
			model.addElement("adp");
			model.addElement("adv");
			model.addElement("conj");
			model.addElement("par");
			
		cbxPOS.setModel(model);
	}
	
	public Lexicon getSelectedWord(){
		for(Lexicon lex : lexList.getLexiconList()){
			if((lex.getName()+" - "+ lex.getGloss()).equals(cbxWord.getSelectedItem().toString())){
				return lex;
			}
		}
		
		return null;
	}
	
	public String getSelectedFromPOS(){
		return cbxPOS.getSelectedItem().toString();
	}
	
	public void setCancelListener(ActionListener AL)
	{
		cancelBtn.addActionListener(AL);
	}
	
	public void setOkListener(ActionListener AL){
		okBtn.addActionListener(AL);
	}
	
	public void setAddNewWordListener(ActionListener AL){
		newWordBtn.addActionListener(AL);
	}
	
}
